const baseURL = import.meta.env.VITE_BASE_URL; 
const baseFileURL = import.meta.env.VITE_BASE_URL;
// request.js
// 定义 API 基地址
// const baseURL = 'https://center.baiyiyl.com/baiyi';
// const baseURL = 'http://172.16.2.168:9011/baiyi';
// const baseFileURL = 'https://center.baiyiyl.com/byyl/files/' 

// 获取 token 的函数，这里假设从本地存储中获取
const getToken = () => {
	return uni.getStorageSync('token');
};

// 封装 get 请求
const get = (url, params = {}) => {
	const fullURL = baseURL + url;
	const token = getToken();

	return new Promise((resolve, reject) => {
		uni.request({
			url: fullURL,
			method: 'GET',
			data: params,
			header: {
				'token': token
			},
			success: (res) => {
				console.log("res", res)
				if (res.data.code === 0) {
					resolve(res.data);
				} else {
					reject(res.data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 封装 post 请求
const post = (url, data = {}) => {
	const fullURL = baseURL + url;
	const token = getToken();

	return new Promise((resolve, reject) => {
		uni.request({
			url: fullURL,
			method: 'POST',
			data,
			header: {
				'token': token
			},
			success: (res) => {
				console.log("res", res)
				if (res.data.code === 0) {
					resolve(res.data);
				} else {
					reject(res.data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

export {
	get,
	post,
	baseFileURL
};